package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController{
    @Autowired
    AdminCouponService adminCouponService;
    @GetMapping("read")
    private Object read(HttpServletRequest req)  {
            String id = req.getParameter("id");
            MarketCoupon couponList=adminCouponService.read(id);
            Object o = ResponseUtil.ok(couponList);
            return o;

    }
    @GetMapping("listUser")
    private Object listUser(HttpServletRequest req)  {
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            String couponId = req.getParameter("couponId");
            String userId =req.getParameter("userId");
            String status = req.getParameter("status");
            List<MarketCouponUser> couponList=adminCouponService.couponlist(page,limit,sort,order,couponId,userId,status);
            Object o = ResponseUtil.okList(couponList);
            return o;

    }
    @GetMapping("list")
    private Object list(HttpServletRequest req)  {
            String sort = req.getParameter("sort");
            String order = req.getParameter("order");
            String page=req.getParameter("page");
            String limit=req.getParameter("limit");
            String name=req.getParameter("name");
            String type=req.getParameter("type");
            String status=req.getParameter("status");
            List<MarketCoupon> couponList=adminCouponService.list(page, limit, sort, order,name,type,status);
            Object o = ResponseUtil.okList(couponList);
          return o;
    }

    @PostMapping("delete")
    private Object delete(@RequestBody MarketCoupon marketCoupon) {
            Integer i=adminCouponService.deleteCoupon(marketCoupon);
            if(i>0){
                Object ok = ResponseUtil.ok();
                return ok;
            }
            Object fail=ResponseUtil.fail();
            return fail;

    }

    @PostMapping("update")
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String jsonStr =req.getReader().readLine();
            ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
            MarketCoupon marketCoupon = objectMapper.readValue(jsonStr, MarketCoupon.class);
            marketCoupon.setUpdateTime(LocalDateTime.now());
            Integer i=adminCouponService.updateCoupon(marketCoupon);
            if(i>0){
                Object ok = ResponseUtil.ok(marketCoupon);
                String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
                resp.getWriter().write(s);
                return;
            }
            Object fail=ResponseUtil.fail();
            String f=JacksonUtil.getObjectMapper().writeValueAsString(fail);
            resp.getWriter().write(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @PostMapping("create")
    private Object create(HttpServletRequest req) throws IOException {
        String jsonStr =req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketCoupon marketCoupon = objectMapper.readValue(jsonStr, MarketCoupon.class);
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());
        Integer i = adminCouponService.addCoupon(marketCoupon);
        if (i > 0) {
            Object ok = ResponseUtil.ok(marketCoupon);

            return ok;
        }
        Object fail = ResponseUtil.fail();
        return fail;
    }
}
